// ---------------------------------------------------------------------
//
// -- 用户信息基类
// -- author £
//
// ---------------------------------------------------------------------

cc.Class({
    extends: cc.Component,

    properties: {
        numScore: require('BaseNum'),
        lbScore: cc.Label,
        lbName: cc.Label,
        lbProvince: cc.Label,
        head: cc.Sprite,
        genderSp: cc.Sprite,
        genderBg: cc.Sprite,
        genders: {
            default: [],
            type: cc.SpriteFrame
        },
        genderBgs: {
            default: [],
            type: cc.SpriteFrame
        },
        // disconnect: cc.Node,
        // ready: cc.Node,
        // index: 0,
        pos: 0,

        score: {
            set: function (value) {
                if (this.numScore)
                    this.numScore.updateNum(
                        this.pos === 0 ? '$' + value : value + '$');

                if (this.lbScore)
                    this.lbScore.string = value + '';
            }
        },
        nickname: {
            set: function (value) {
                this.lbName.string = value;
            }
        },
        province: {
            set: function (value) {
                this.lbProvince.string = value;
            }
        },
        // isDisconnected: {
        //     set: function (value) {
        //         this.disconnect.active = value;

        //         if (value) {
        //             this.disconnect.stopAllActions();
        //             this.disconnect.opacity = 0;
        //             this.disconnect.y = this._discInitY - this.disconnect.height;
        //             this.disconnect.runAction(
        //                 cc.spawn(
        //                         cc.fadeIn(this.DSHOWTIME),
        //                         cc.moveTo(this.DSHOWTIME, this.disconnect.x, this._discInitY)
        //                     )
        //             );
        //         }
        //     }
        // },
        // isReady: {
        //     set: function (value) {
        //         this.ready.active = value;

        //         if (value) {
        //             this.ready.stopAllActions();
        //             this.ready.opacity = 0;
        //             this.ready.y = this._readyInitY - this.ready.height;
        //             this.ready.runAction(
        //                 cc.spawn(
        //                         cc.fadeIn(this.DSHOWTIME),
        //                         cc.moveTo(this.DSHOWTIME, this.ready.x, this._readyInitY)
        //                     )
        //             );
        //         }
        //     }
        // },
    },

    // use this for initialization
    onLoad: function () {
        // this.isDisconnected = false;
        // this.isReady = false;
        if (this.head)
            this._defaultHead = this.head.spriteFrame;

        if (this.genderSp)
            this.genderSp.node.active = false;

        if (this.pos === 0 && !require('AppInfo').wx) {
            require('TextureManager').getTexture(
                'common', '', 'head' + this.pos, this.head
            );
        }
    },

    start: function () {
    },

    setHead: function (url, id) {
        require('Platform').setHead(url, id, this.head);
    },

    setData: function (data) {
        if (!data) return; 

        // this.isReady = data.isReady;
        // this.isDisconnected = data.isDisconnected;

        if (data.headimgurl && 
            data.userid !== null && data.userid !== undefined && 
            this.head)
            this.setHead(data.headimgurl, data.userid);

        if (data.nickname && this.lbName)
            this.nickname = data.nickname;

        if (data.province && this.lbProvince)
            this.province = data.province;

        if (!data.skipScore)
            this.score = data.score ? data.score : 0;  

        if (data.sex !== null && data.sex !== undefined) {
            if (this.genderSp)
                this.genderSp.node.active = true;
            if (this.genderSp)
                this.genderSp.spriteFrame = this.genders[data.sex === 1 ? 0 : 1];
            if (this.genderBg)
                this.genderBg.spriteFrame = this.genderBgs[data.sex === 1 ? 0 : 1];
        }
    },

    reset: function () {
        if (this.head)
            this.head.spriteFrame = this._defaultHead;
        if (this.genderSp)
            this.genderSp.spriteFrame = this.genders[0];
        if (this.genderSp)
            this.genderSp.node.active = false;
        if (this.genderBg)
            this.genderBg.spriteFrame = this.genderBgs[0];
        if (this.numScore)
            this.numScore.clear();
        if (this.lbName)
            this.nickname = '';
        if (this.lbProvince)
            this.province = '';
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
